/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;


import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Kyle
 */
public class KeyDefinitions {
    //Note key[0] = keycode, key[1] = modifier
	/****
	 * GAME PLAY CONTROLS
	 *****/
    //Cycle
    private int[] modePrev = new int[2];
    private int[] modeNext = new int[2];
    private int[] typePrev = new int[2];
    private int[] typeNext = new int[2];
    private int[] instancePrev = new int[2];
    private int[] instanceNext = new int[2];
    private int[] commandPrev = new int[2];
    private int[] commandNext = new int[2];

    //Direction
    private int[] North = new int[2];
    private int[] NorthEast = new int[2];
    private int[] NorthWest = new int[2];
    private int[] South = new int[2];
    private int[] SouthEast = new int[2];
    private int[] SouthWest = new int[2];

    //Selection
    private int[] Select1 = new int[2];
    private int[] Select2 = new int[2];
    private int[] Select3 = new int[2];
    private int[] Select4 = new int[2];
    private int[] Select5 = new int[2];
    private int[] Select6 = new int[2];
    private int[] Select7 = new int[2];
    private int[] Select8 = new int[2];
    private int[] Select9 = new int[2];
    private int[] Select0 = new int[2];

    //Other
    private int[] Focus = new int[2];
    private int[] Issue = new int[2];
    private int[] Pause = new int[2];
    private int[] editControls = new int[2];
    private int[] Tab = new int[2];
    private int[] createRally = new int[2];
    private int[] Cancel = new int[2];
    private int[] switchView = new int[2];
    private int[] switchToTree = new int[2];

    public KeyDefinitions(){
        this.restoreDefault();
    }

    public void restoreDefault(){
        //cycle
        modePrev[0] = 40;
        modePrev[1] = 2;

        modeNext[0] = 38;
        modeNext[1] = 2;

        typePrev[0] = 37;
        typePrev[1] = 2;

        typeNext[0] = 39;
        typeNext[1] = 2;

        instancePrev[0] = 37;
        instancePrev[1] = 0;

        instanceNext[0] = 39;
        instanceNext[1] = 0;

        commandPrev[0] = 40;
        commandPrev[1] = 0;

        commandNext[0] = 38;
        commandNext[1] = 0;

        //direction
        North[0] = 104;
        North[1] = 0;

        NorthEast[0] = 105;
        NorthEast[1] = 0;

        NorthWest[0] = 103;
        NorthWest[1] = 0;

        South[0] = 98;
        South[1] = 0;

        SouthEast[0] = 99;
        SouthEast[1] = 0;

        SouthWest[0] = 97;
        SouthWest[1] = 0;

        //Selection
        Select1[0] = 49;
        Select1[1] = 0;

        Select2[0] = 50;
        Select2[1] = 0;

        Select3[0] = 51;
        Select3[1] = 0;

        Select4[0] = 52;
        Select4[1] = 0;

        Select5[0] = 53;
        Select5[1] = 0;

        Select6[0] = 54;
        Select6[1] = 0;

        Select7[0] = 55;
        Select7[1] = 0;

        Select8[0] = 56;
        Select8[1] = 0;

        Select9[0] = 57;
        Select9[1] = 0;

        Select0[0] = 48;
        Select0[1] = 0;

        //Other
        Focus[0] = 101;
        Focus[1] = 0;

        Issue[0] = 10;
        Issue[1] = 0;
        
        Pause[0] = 80;
        Pause[1] = 0;
        
        editControls[0] = 67;
        editControls[1] = 2;
        //Actually back quote(lowercase of tilde key), Tab has no keycode or keymod
        Tab[0] = 192;
        Tab[1] = 0;
        
        createRally[0] = 82;
        createRally[1] = 0;
        
        Cancel[0] = 27;
        Cancel[1] = 0;
        
        switchView[0] = 79;
        switchView[1] = 2;
        
        switchToTree[0] = 84;
        switchToTree[1] = 2;
    }

    public int[] getModePrev(){return modePrev;}
    public int[] getModeNext(){return modeNext;}
    public int[] getTypePrev(){return typePrev;}
    public int[] getTypeNext(){return typeNext;}
    public int[] getInstancePrev(){return instancePrev;}
    public int[] getInstanceNext(){return instanceNext;}
    public int[] getCommandPrev(){return commandPrev;}
    public int[] getCommandNext(){return commandNext;}
    public int[] getNorth(){return North;}
    public int[] getNorthEast(){return NorthEast;}
    public int[] getNorthWest(){return NorthWest;}
    public int[] getSouth(){return South;}
    public int[] getSouthEast(){return SouthEast;}
    public int[] getSouthWest(){return SouthWest;}
    public int[] getSelect1(){return Select1;}
    public int[] getSelect2(){return Select2;}
    public int[] getSelect3(){return Select3;}
    public int[] getSelect4(){return Select4;}
    public int[] getSelect5(){return Select5;}
    public int[] getSelect6(){return Select6;}
    public int[] getSelect7(){return Select7;}
    public int[] getSelect8(){return Select8;}
    public int[] getSelect9(){return Select9;}
    public int[] getSelect0(){return Select0;}
    public int[] getFocus(){return Focus;}
    public int[] getIssue(){return Issue;}
    public int[] getPause(){return Pause;}
    public int[] getEditControls(){return editControls;}
    public int[] getTab(){return Tab;}
    public int[] getCreateRally(){return createRally;}
    public int[] getCancel(){return Cancel;}
    public int[] getSwitchView(){return switchView;}
    public int[] getSwitchToTree(){return switchToTree;}
    
    private boolean checkAvailable(int[] key){
        if(Arrays.equals(modePrev,key))
            return false;
        if(Arrays.equals(modeNext,key))
             return false;
        if(Arrays.equals(typePrev,key))
            return false;
        if(Arrays.equals(typeNext,key))
            return false;
        if(Arrays.equals(instancePrev,key))
            return false;
        if(Arrays.equals(instanceNext,key))
            return false;
        if(Arrays.equals(commandPrev,key))
            return false;
        if(Arrays.equals(commandNext,key))
            return false;
        if(Arrays.equals(North,key))
            return false;
        if(Arrays.equals(NorthEast,key))
            return false;
        if(Arrays.equals(NorthWest,key))
            return false;
        if(Arrays.equals(South,key))
            return false;
        if(Arrays.equals(SouthEast,key))
            return false;
        if(Arrays.equals(SouthWest,key))
            return false;
        if(Arrays.equals(Select1,key))
            return false;
        if(Arrays.equals(Select2,key))
            return false;
        if(Arrays.equals(Select3,key))
            return false;
        if(Arrays.equals(Select4,key))
            return false;
        if(Arrays.equals(Select5,key))
            return false;
        if(Arrays.equals(Select6,key))
            return false;
        if(Arrays.equals(Select7,key))
            return false;
        if(Arrays.equals(Select8,key))
            return false;
        if(Arrays.equals(Select9,key))
            return false;
        if(Arrays.equals(Select0,key))
            return false;
        if(Arrays.equals(Focus,key))
            return false;
        if(Arrays.equals(Issue,key))
            return false;
        if(Arrays.equals(Pause, key))
        	return false;
        if(Arrays.equals(editControls, key))
        	return false;
        if(Arrays.equals(Tab, key))
        	return false;
        if(Arrays.equals(createRally, key))
        	return false;
        if(Arrays.equals(Cancel, key))
        	return false;
        if(Arrays.equals(switchView, key))
        	return false;
        if(Arrays.equals(switchToTree, key))
        	return false;
        return true;

    }
    public void setModePrev(int[] key){if(checkAvailable(key))this.modePrev = key;}
    public void setModeNext(int[] key){if(checkAvailable(key))this.modeNext = key;}
    public void setTypePrev(int[] key){if(checkAvailable(key))this.typePrev = key;}
    public void setTypeNext(int[] key){if(checkAvailable(key))this.typeNext = key;}
    public void setInstancePrev(int[] key){if(checkAvailable(key))this.instancePrev = key;}
    public void setInstanceNext(int[] key){if(checkAvailable(key))this.instanceNext = key;}
    public void setCommandPrev(int[] key){if(checkAvailable(key))this.commandPrev = key;}
    public void setCommandNext(int[] key){if(checkAvailable(key))this.commandNext = key;}
    public void setNorth(int[] key){if(checkAvailable(key))this.North = key;}
    public void setNorthEast(int[] key){if(checkAvailable(key))this.NorthEast = key;}
    public void setNorthWest(int[] key){if(checkAvailable(key))this.NorthWest = key;}
    public void setSouth(int[] key){if(checkAvailable(key))this.South = key;}
    public void setSouthEast(int[] key){if(checkAvailable(key))this.SouthEast = key;}
    public void setSouthWest(int[] key){if(checkAvailable(key))this.SouthWest = key;}
    public void setSelect1(int[] key){if(checkAvailable(key))this.Select1 = key;}
    public void setSelect2(int[] key){if(checkAvailable(key))this.Select2 = key;}
    public void setSelect3(int[] key){if(checkAvailable(key))this.Select3 = key;}
    public void setSelect4(int[] key){if(checkAvailable(key))this.Select4 = key;}
    public void setSelect5(int[] key){if(checkAvailable(key))this.Select5 = key;}
    public void setSelect6(int[] key){if(checkAvailable(key))this.Select6 = key;}
    public void setSelect7(int[] key){if(checkAvailable(key))this.Select7 = key;}
    public void setSelect8(int[] key){if(checkAvailable(key))this.Select8 = key;}
    public void setSelect9(int[] key){if(checkAvailable(key))this.Select9 = key;}
    public void setSelect0(int[] key){if(checkAvailable(key))this.Select0 = key;}
    public void setFocus(int[] key){if(checkAvailable(key))this.Focus = key;}
    public void setIssue(int[] key){if(checkAvailable(key))this.Issue = key;}
    public void setPause(int[] key){if(checkAvailable(key))this.Pause = key;}
    public void setEditControls(int[] key){if(checkAvailable(key))this.editControls = key;}
    public void setTab(int[] key){if(checkAvailable(key))this.Tab = key;}
    public void setCreateRally(int[] key){if(checkAvailable(key))this.createRally = key;}
    public void setCancel(int[] key){if(checkAvailable(key))this.Cancel = key;}
    public void setSwitchView(int[] key){if(checkAvailable(key))this.switchView = key;}
    public void setSwitchToTree(int[] key){if(checkAvailable(key))this.switchToTree = key;}
    
    public String toStringModePrev(){return KeyEvent.getKeyModifiersText(modePrev[1]) +" "+ KeyEvent.getKeyText(modePrev[0]);}
    public String toStringModeNext(){return KeyEvent.getKeyModifiersText(modeNext[1]) +" "+ KeyEvent.getKeyText(modeNext[0]);}
    public String toStringTypePrev(){return KeyEvent.getKeyModifiersText(typePrev[1]) +" "+ KeyEvent.getKeyText(typePrev[0]);}
    public String toStringTypeNext(){return KeyEvent.getKeyModifiersText(typeNext[1]) +" "+ KeyEvent.getKeyText(typeNext[0]);}
    public String toStringInstancePrev(){return KeyEvent.getKeyModifiersText(instancePrev[1]) +" "+ KeyEvent.getKeyText(instancePrev[0]);}
    public String toStringInstanceNext(){return KeyEvent.getKeyModifiersText(instanceNext[1]) +" "+ KeyEvent.getKeyText(instanceNext[0]);}
    public String toStringCommandPrev(){return KeyEvent.getKeyModifiersText(commandPrev[1]) +" "+ KeyEvent.getKeyText(commandPrev[0]);}
    public String toStringCommandNext(){return KeyEvent.getKeyModifiersText(commandNext[1]) +" "+ KeyEvent.getKeyText(commandNext[0]);}
    public String toStringNorth(){return KeyEvent.getKeyModifiersText(North[1]) +" "+ KeyEvent.getKeyText(North[0]);}
    public String toStringNorthEast(){return KeyEvent.getKeyModifiersText(NorthEast[1]) +" "+ KeyEvent.getKeyText(NorthEast[0]);}
    public String toStringNorthWest(){return KeyEvent.getKeyModifiersText(NorthWest[1]) +" "+ KeyEvent.getKeyText(NorthWest[0]);}
    public String toStringSouth(){return KeyEvent.getKeyModifiersText(South[1]) +" "+ KeyEvent.getKeyText(South[0]);}
    public String toStringSouthEast(){return KeyEvent.getKeyModifiersText(SouthEast[1]) +" "+ KeyEvent.getKeyText(SouthEast[0]);}
    public String toStringSouthWest(){return KeyEvent.getKeyModifiersText(SouthWest[1]) +" "+ KeyEvent.getKeyText(SouthWest[0]);}
    public String toStringSelect1(){return KeyEvent.getKeyModifiersText(Select1[1]) +" "+ KeyEvent.getKeyText(Select1[0]);}
    public String toStringSelect2(){return KeyEvent.getKeyModifiersText(Select2[1]) +" "+ KeyEvent.getKeyText(Select2[0]);}
    public String toStringSelect3(){return KeyEvent.getKeyModifiersText(Select3[1]) +" "+ KeyEvent.getKeyText(Select3[0]);}
    public String toStringSelect4(){return KeyEvent.getKeyModifiersText(Select4[1]) +" "+ KeyEvent.getKeyText(Select4[0]);}
    public String toStringSelect5(){return KeyEvent.getKeyModifiersText(Select5[1]) +" "+ KeyEvent.getKeyText(Select5[0]);}
    public String toStringSelect6(){return KeyEvent.getKeyModifiersText(Select6[1]) +" "+ KeyEvent.getKeyText(Select6[0]);}
    public String toStringSelect7(){return KeyEvent.getKeyModifiersText(Select7[1]) +" "+ KeyEvent.getKeyText(Select7[0]);}
    public String toStringSelect8(){return KeyEvent.getKeyModifiersText(Select8[1]) +" "+ KeyEvent.getKeyText(Select8[0]);}
    public String toStringSelect9(){return KeyEvent.getKeyModifiersText(Select9[1]) +" "+ KeyEvent.getKeyText(Select9[0]);}
    public String toStringSelect0(){return KeyEvent.getKeyModifiersText(Select0[1]) +" "+ KeyEvent.getKeyText(Select0[0]);}
    public String toStringFocus(){return KeyEvent.getKeyModifiersText(Focus[1]) +" "+ KeyEvent.getKeyText(Focus[0]);}
    public String toStringIssue(){return KeyEvent.getKeyModifiersText(Issue[1]) +" "+ KeyEvent.getKeyText(Issue[0]);}
    public String toStringPause(){return KeyEvent.getKeyModifiersText(Pause[1]) +" "+ KeyEvent.getKeyText(Pause[0]);}
    public String toStringEditControls(){return KeyEvent.getKeyModifiersText(editControls[1]) +" "+ KeyEvent.getKeyText(editControls[0]);}
    public String toStringTab(){return KeyEvent.getKeyModifiersText(Tab[1]) +" "+ KeyEvent.getKeyText(Tab[0]);}
    public String toStringCreateRally(){return KeyEvent.getKeyModifiersText(createRally[1]) +" "+ KeyEvent.getKeyText(createRally[0]);}
    public String toStringCancel(){return KeyEvent.getKeyModifiersText(Cancel[1]) +" "+ KeyEvent.getKeyText(Cancel[0]);}
    public String toStringSwitchView(){return KeyEvent.getKeyModifiersText(switchView[1]) +" "+ KeyEvent.getKeyText(switchView[0]);}
    public String toStringSwitchtoTree(){return KeyEvent.getKeyModifiersText(switchToTree[1]) +" "+ KeyEvent.getKeyText(switchToTree[0]);}
    
    public void loadKeyDefinitions(File keyDefs){
        try{
            Scanner scanner = new Scanner(keyDefs);
            while(scanner.hasNext()){
                String command = scanner.next();
                int[] key = new int[2];
                key[0] = scanner.nextInt();
                key[1] = scanner.nextInt();
                if(command.equals("modePrev")){setModePrev(key);}
                else if(command.equals("modeNext")){setModeNext(key);}
                else if(command.equals("typePrev")){setTypePrev(key);}
                else if(command.equals("typeNext")){setTypeNext(key);}
                else if(command.equals("instancePrev")){setInstancePrev(key);}
                else if(command.equals("instanceNext")){setInstanceNext(key); }
                else if(command.equals("commandPrev")){setCommandPrev(key);}
                else if(command.equals("commandNext")){setCommandNext(key); }
                else if(command.equals("North")){setNorth(key);}
                else if(command.equals("NorthEast")){setNorthEast(key);}
                else if(command.equals("NorthWest")){setNorthWest(key);}
                else if(command.equals("South")){setSouth(key);}
                else if(command.equals("SouthEast")){setSouthEast(key);}
                else if(command.equals("SouthWest")){setSouthWest(key);}
                else if(command.equals("Select1")){setSelect1(key);}
                else if(command.equals("Select2")){setSelect2(key);}
                else if(command.equals("Select3")){setSelect3(key);}
                else if(command.equals("Select4")){setSelect4(key);}
                else if(command.equals("Select5")){setSelect5(key);}
                else if(command.equals("Select6")){setSelect6(key);}
                else if(command.equals("Select7")){setSelect7(key);}
                else if(command.equals("Select8")){setSelect8(key);}
                else if(command.equals("Select9")){setSelect9(key);}
                else if(command.equals("Select0")){setSelect0(key);}
                else if(command.equals("Focus")){setFocus(key);}
                else if(command.equals("Issue")){setIssue(key);}
                else if(command.equals("Pause")){setPause(key);}
                else if(command.equals("editControls")){setEditControls(key);}
                else if(command.equals("Tab")){setEditControls(key);}
                else if(command.equals("createRally")){setCreateRally(key);}
                else if(command.equals("Cancel")){setCancel(key);}
                else if(command.equals("switchView")){setSwitchView(key);}
                else if(command.equals("switchToTree")){setSwitchToTree(key);}
            }
            scanner.close();
        }
        catch(Exception e){
        	System.out.println("Exception");
        	//System.out.println(e.fillInStackTrace());
        }

    }

    public void saveKeyDefinitions(File keyDefs){
        try{
            BufferedWriter writer = new BufferedWriter(new FileWriter(keyDefs));
            writer.write("modePrev" + " " + modePrev[0] + " " + modePrev[1] + "\n");
            writer.write("modeNext" + " " + modeNext[0] + " " + modeNext[1] + "\n");
            writer.write("typePrev" + " " + typePrev[0] + " " + typePrev[1] + "\n");
            writer.write("typeNext" + " " + typeNext[0] + " " + typeNext[1] + "\n");
            writer.write("instancePrev" + " " + instancePrev[0] + " " + instancePrev[1] + "\n");
            writer.write("instanceNext" + " " + instanceNext[0] + " " + instanceNext[1] + "\n");
            writer.write("commandPrev" + " " + commandPrev[0] + " " + commandPrev[1] + "\n");
            writer.write("commandNext" + " " + commandNext[0] + " " + commandNext[1] + "\n");
            writer.write("North" + " " + North[0] + " " + North[1] + "\n");
            writer.write("NorthEast" + " " + NorthEast[0] + " " + NorthEast[1] + "\n");
            writer.write("NorthWest" + " " + NorthWest[0] + " " + NorthWest[1] + "\n");
            writer.write("South" + " " + South[0] + " " + South[1] + "\n");
            writer.write("SouthEast" + " " + SouthEast[0] + " " + SouthEast[1] + "\n");
            writer.write("SouthWest" + " " + SouthWest[0] + " " + SouthWest[1] + "\n");
            writer.write("Select1" + " " + Select1[0] + " " + Select1[1] + "\n");
            writer.write("Select2" + " " + Select2[0] + " " + Select2[1] + "\n");
            writer.write("Select3" + " " + Select3[0] + " " + Select3[1] + "\n");
            writer.write("Select4" + " " + Select4[0] + " " + Select4[1] + "\n");
            writer.write("Select5" + " " + Select5[0] + " " + Select5[1] + "\n");
            writer.write("Select6" + " " + Select6[0] + " " + Select6[1] + "\n");
            writer.write("Select7" + " " + Select7[0] + " " + Select7[1] + "\n");
            writer.write("Select8" + " " + Select8[0] + " " + Select8[1] + "\n");
            writer.write("Select9" + " " + Select9[0] + " " + Select9[1] + "\n");
            writer.write("Select0" + " " + Select0[0] + " " + Select0[1] + "\n");
            writer.write("Focus" + " " + Focus[0] + " " + Focus[1] + "\n");
            writer.write("Issue" + " " + Issue[0] + " " + Issue[1] + "\n");
            writer.write("Pause" + " " + Pause[0] + " " + Pause[1] + "\n");
            writer.write("editControls" + " " + editControls[0] + " " + editControls[1] + "\n");
            writer.write("Tab" + " " + Tab[0] + " " + Tab[1] + "\n");
            writer.write("createRally" + " " + createRally[0] + " " + createRally[1] + "\n");
            writer.write("Cancel" + " " + Cancel[0] + " " + Cancel[1] + "\n");
            writer.write("switchView" + " " + switchView[0] + " " + switchView[1] + "\n");
            writer.write("switchToTree" + " " + switchToTree[0] + " " + switchToTree[1] + "\n");
            writer.close();
        }
        catch(Exception e){System.out.println(e.toString());}
    }
}
